package service

import (
	"sort"
	"time"
	"xueyuan_backend/model"
)

func CollectRecord(user_id string) (articles []model.Article) {
	articles = GetCollectRecordFromDB(user_id)
	return articles
}

func SearchArticle(keyword string, user_id string, page int) (isCollectArticles []model.CollectArticle) {
	var articles []model.Article
	articles = SearchArticleFromDB(keyword, page)

	//时间从前到后返回
	// sort.Sort(ArticleSlice(articles))

	collectedArticles := GetCollectRecordFromDB(user_id)
	var ica model.CollectArticle
	for _, a := range articles {
		ica.Title = a.Title
		ica.Date = a.Date
		ica.Content = a.Content
		ica.ArticleId = a.ArticleId
		ica.PicLink = a.PicLink
		ica.IsCollected = false
		ica.SourceName = a.SourceName
		for _, ca := range collectedArticles {
			if a.ArticleId == ca.ArticleId {
				ica.IsCollected = true
				break
			}
		}
		isCollectArticles = append(isCollectArticles, ica)
	}

	return isCollectArticles
}

func SearchCollectArticle(keyword string, user_id string) (isCollectArticles []model.CollectArticle) {
	var articles []model.Article
	articles = SearchCollectArticleFromDB(keyword, user_id)

	//时间从前到后返回
	sort.Sort(ArticleSlice(articles))

	var ica model.CollectArticle
	for _, a := range articles {
		ica.Title = a.Title
		ica.Date = a.Date
		ica.Content = a.Content
		ica.ArticleId = a.ArticleId
		ica.PicLink = a.PicLink
		ica.IsCollected = true
		ica.SourceName = a.SourceName
		isCollectArticles = append(isCollectArticles, ica)
	}

	return isCollectArticles
}

func ScanArticle(user_id string, web_category string, page int) (isCollectArticles []model.CollectArticle) {
	var articles []model.Article
	articles = ScanArticleFromDB(user_id, web_category, page)
	//时间从前到后返回
	// sort.Sort(ArticleSlice(articles))

	collectedArticles := GetCollectRecordFromDB(user_id)
	var ica model.CollectArticle
	for _, a := range articles {
		ica.Title = a.Title
		ica.Date = a.Date
		ica.Content = a.Content
		ica.ArticleId = a.ArticleId
		ica.PicLink = a.PicLink
		ica.IsCollected = false
		ica.SourceName = a.SourceName
		for _, ca := range collectedArticles {
			if a.ArticleId == ca.ArticleId {
				ica.IsCollected = true
				break
			}
		}
		isCollectArticles = append(isCollectArticles, ica)
	}

	return isCollectArticles
}

func ViewArticle(article_id string) (article model.Article) {
	article = ViewArticleFromDB(article_id)
	return article
}

func AddCollect(user_id string, article_id string) bool {
	if AddCollectToDB(user_id, article_id) == false {
		return false
	}

	return true
}

func DeleteCollect(user_id string, article_id string) bool {
	if DeleteCollectToDB(user_id, article_id) == false {
		return false
	}
	return true
}

type ArticleSlice []model.Article

func (a ArticleSlice) Len() int { // 重写 Len() 方法
	return len(a)
}
func (a ArticleSlice) Swap(i, j int) { // 重写 Swap() 方法
	a[i], a[j] = a[j], a[i]
}
func (a ArticleSlice) Less(i, j int) bool { // 重写 Less() 方法， 从大到小排序
	ti := parse_timestr_to_datetime(a[i].Date)
	tj := parse_timestr_to_datetime(a[j].Date)
	if ti.Before(tj) {
		return false
	}
	return true
}

func parse_timestr_to_datetime(time_str string) time.Time {
	time_str = time_str[0:10]
	t, error := time.Parse("2006-01-02", time_str)
	if error != nil {
		panic(error)
	}
	return t
}
